package controllers

import (
	"app/internal/struct/web"
	"app/internal/web/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserController struct {
	Controller
}

func (Controller *UserController) GetControllerMethod() []string {

	return []string{MethodHttpPost}
}
func (Controller *UserController) GetControllerName() string {

	return "UserController"
}
func (Controller *UserController) GetSchema() string {

	return "/user"
}

type UserRequest struct {
	Token    string `json:"token"`
	web.User `json:"data,omitempty"`
}

func (Controller *UserController) Post(c *gin.Context) {

	obj := UserRequest{}
	res := Response{ErrorCode: 0, ErrorMessage: "", Data: nil}

	if err := c.ShouldBindJSON(&obj); err != nil {
		res.setErrorObject(jsonError)
		res.setErrorMessage(err.Error())
		goto ServiceError
	}

	if err := service.GetServiceInstance().IsExitsUserId(obj.Id); err != nil {
		res.setErrorObject(userIdError)
		goto ServiceError
	}

	if err := service.GetServiceInstance().Heart(obj.Id); err != nil {
		res.setErrorObject(heartError)
		goto ServiceError
	}

	if err := service.GetServiceInstance().RefreshUserInfo(obj.User); err != nil {
		res.setErrorObject(userError)
		goto ServiceError
	}

	Controller.ServiceFinish(c, http.StatusOK, &res)
	return

ServiceError:
	Controller.ServiceFinish(c, http.StatusOK, &res)
	return

}
